package com.csair.seam.domain.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csair.seam.domain.mapper.FaAssetMapper;
import com.csair.seam.domain.model.entity.EquipCategory;
import com.csair.seam.domain.model.entity.EquipOrg;
import com.csair.seam.domain.model.entity.FaAsset;
import com.csair.seam.domain.model.entity.FaAssetDepreciation;
import com.csair.seam.domain.model.param.FaAssetBO;
import com.csair.seam.domain.model.param.FaAssetQueryParam;
import com.csair.seam.domain.service.IEquipCategoryService;
import com.csair.seam.domain.service.IEquipOrgService;
import com.csair.seam.domain.service.IFaAssetService;
import com.csair.seam.infrastructure.config.DictionaryConfigurationProperties;

@Service
public class FaAssetServiceImpl extends ServiceImpl<FaAssetMapper, FaAsset> implements IFaAssetService{
	@Autowired
    private RestTemplate restTemplate;
	
	@Autowired
	private IEquipCategoryService equipCategoryService;
	
	@Autowired
	private IEquipOrgService equipOrgService;
	
	@Autowired
	private DictionaryConfigurationProperties dictionaryConfigurationProperties;

	@Override
	public void updateFaAsset() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Authorization", dictionaryConfigurationProperties.getFaToken());
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
	    factory.setReadTimeout(30*60*1000);//单位为ms
	    restTemplate.setRequestFactory(factory);
		
		List<EquipOrg> equipOrgList = equipOrgService.findEquipOrgList();
		
		List<EquipCategory> equipCategoryList = equipCategoryService.findLevel1CategoryList(null);
		Set<String> bookTypeCodeSet = equipOrgList.stream().map(EquipOrg::getBookTypeCode).collect(Collectors.toSet());
		for (EquipCategory equipCategory : equipCategoryList) {
			FaAssetBO bo = new FaAssetBO();
			bo.setSegment1(equipCategory.getCategoryCode());//资产大类
			if(CollectionUtils.isNotEmpty(equipOrgList)) {
				for (String bookTypeCode : bookTypeCodeSet) {
					bo.setBookTypeCode(bookTypeCode);//账簿
					String json = JSON.toJSONString(bo);
					HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
					String result = restTemplate.postForObject(dictionaryConfigurationProperties.getFaUrl(), formEntity, String.class);
					List<FaAsset> list = JSONObject.parseArray(result, FaAsset.class);
					//同步到数据库
					if(CollectionUtils.isNotEmpty(list)) {
						saveBatch(list, 10000);
					}
				}
			}else {
				String json = JSON.toJSONString(bo);
				HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
				String result = restTemplate.postForObject(dictionaryConfigurationProperties.getFaUrl(), formEntity, String.class);
				List<FaAsset> list = JSONObject.parseArray(result, FaAsset.class);
				//同步到数据库
				if(CollectionUtils.isNotEmpty(list)) {
					saveBatch(list, 10000);
				}
			}
		}
		
	}

	@Override
	public List<FaAssetDepreciation> queryFaAssetDepreciation(FaAssetQueryParam param) {
		return getBaseMapper().queryFaAssetDepreciation(param);
	}

	@Override
	public Page<FaAssetDepreciation> queryFaAssetDepreciationPage(FaAssetQueryParam param) {
		Page<FaAssetDepreciation> page = new Page<FaAssetDepreciation>(param.getPageIndex(), param.getPageSize());
		page = getBaseMapper().queryFaAssetDepreciation(page, param);
		return page;
	}

}
